package com.bootdo.market.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.common.HistoryTodayRedis;
import com.bootdo.market.dao.CommonDao;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.ProductAdvertisementDO;
import com.bootdo.market.domain.ProductChannelRelationshipDO;
import com.bootdo.market.service.ProductAdvertisementService;
import com.bootdo.market.service.ProductChannelRelationshipService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-15 16:23:12
 */

@Controller
@RequestMapping("/market/productChannelRelationship")
public class ProductChannelRelationshipController {
    @Autowired
    private ProductChannelRelationshipService productChannelRelationshipService;
    @Autowired
    private CommonDao commonDao;
    @Autowired
    private ProductAdvertisementService productAdvertisementService;
    @Autowired
    private HistoryTodayRedis historyTodayRedis;
    @Autowired
    private DuplicateDao duplicateDao;

    @GetMapping()
    @RequiresPermissions("market:productChannelRelationship:productChannelRelationship")
    String ProductChannelRelationship(Integer id, Model model) {
        model.addAttribute("channelid", id);
        return "market/productChannelRelationship/productChannelRelationship";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:productChannelRelationship:productChannelRelationship")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProductChannelRelationshipDO> productChannelRelationshipList = productChannelRelationshipService.list(query);
        productChannelRelationshipList.stream().forEach(productChannelRelationshipDO -> {
            productChannelRelationshipDO.setProductDomain(productChannelRelationshipDO.getProductDomain() + "?ch_id=" + productChannelRelationshipDO.getChannelid() + "&linkname=" + productChannelRelationshipDO.getLinkname());
            productChannelRelationshipDO.setRate(productChannelRelationshipDO.getChannelsettlementrate() == null ? "" : productChannelRelationshipDO.getChannelsettlementrate() + ":" + (productChannelRelationshipDO.getPlatformsettlementrate() == null ? "" : productChannelRelationshipDO.getPlatformsettlementrate()));
        });
        int total = productChannelRelationshipService.count(query);
        PageUtils pageUtils = new PageUtils(productChannelRelationshipList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:productChannelRelationship:add")
    String add(Integer channelid, Model model) {
        model.addAttribute("channelid", channelid);
        return "market/productChannelRelationship/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:productChannelRelationship:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ProductChannelRelationshipDO productChannelRelationship = productChannelRelationshipService.get(id);
        model.addAttribute("productChannelRelationship", productChannelRelationship);
        return "market/productChannelRelationship/edit";
    }

    /**
     * 保存
     */
    @Transactional
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:productChannelRelationship:add")
    public R save(ProductChannelRelationshipDO productChannelRelationship) {
        if (productChannelRelationshipService.save(productChannelRelationship) > 0) {
            if (productChannelRelationship.getProductidischanneltemplate() != null) {
                ProductChannelRelationshipDO aa = productChannelRelationshipService.get(productChannelRelationship.getProductidischanneltemplate());
                if (aa != null) {
                    List<ProductAdvertisementDO> productAdvertisementList = commonDao.productAdvertisementDOList(aa);
                    if (productAdvertisementList != null && productAdvertisementList.size() > 0) {
                        productAdvertisementList.stream().forEach(productAdvertisement -> {
                            productAdvertisement.setChannelid(Integer.valueOf(productChannelRelationship.getChannelid()));
                            int advid = productAdvertisement.getId();
                            productAdvertisementService.save(productAdvertisement);
                            productAdvertisementService.update(productAdvertisement);
                            List<Map<String, Object>> mapList = commonDao.findAreaByOfferId(advid);
                            if (mapList != null && mapList.size() > 0) {
                                mapList.stream().forEach(stringObjectMap -> {
                                    stringObjectMap.put("offer_id", productAdvertisement.getId());
                                });
                                productAdvertisementService.insertOfferArea(mapList);
                            }
                            ProductAdvertisementDO aaaa = productAdvertisementService.get(productAdvertisement.getId());
                            ProductAdvertisementController productAdvertisementController = new ProductAdvertisementController();
                            productAdvertisementController.updateAdvertisementDataIntoRedis(aaaa);
                        /*    if (aaaa.getIsdelete() == 1 && aaaa.getPublishstate() == 1) {
                                if (aaaa.getIsdirectional() == 1) {
                                    List<String> bb = commonDao.findAreaIntoRedisOfferId(aaaa.getId());
                                    aaaa.setEdareas(JSON.toJSONString(bb));
                                } else {
                                    List<String> bb = new ArrayList<>();
                                    aaaa.setEdareas(JSON.toJSONString(bb));
                                }
                                historyTodayRedis.saveProductAdvertisementRedis(aaaa);
                            } else {
                                historyTodayRedis.deleteProductAdvertisementRedis(aaaa);
                            }*/
                        });
                    }
                }
            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:productChannelRelationship:edit")
    public R update(ProductChannelRelationshipDO productChannelRelationship) {
        productChannelRelationshipService.update(productChannelRelationship);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:productChannelRelationship:remove")
    public R remove(ProductChannelRelationshipDO productChannelRelationship) {
        productChannelRelationshipService.update(productChannelRelationship);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:productChannelRelationship:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        productChannelRelationshipService.batchRemove(ids);
        return R.ok();
    }

    @RequestMapping(value = "/productChannelRelationshipUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productChannelRelationshipUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productChannelRelationshipUniqueAdd(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/productChannelRelationshipUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productChannelRelationshipUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productChannelRelationshipUniqueEdit(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
